// home.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    autoplay: true,
    interval: 3000,
    duration: 1000,
    categories:[],
    coachcategoryid:0,
    articlecategoryid:0,
    coaches:[],
    envpic:[],
    articles:[],
    aboutusfirst: '关于我们，请关注我们的服务。'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    var that = this;
    wx.setNavigationBarTitle({
      title: wx.getStorageSync('mallName')
    });

    //滚动图
    wx.request({
      url: app.globalData.surl +'/banner/list',
      data: {
        jkum : app.globalData.jkum
      },
      success: function (res) {
        var banners = [];
        var envpics = [];
        var material = {
          id: '',
          url: ''
        };
        for (var i = 0; i < res.data.data.length; i++){
          //主页1
          if (res.data.data[i].creatorType == "1"){
            var m1 = res.data.data[i];
            material.id = m1.id;
            material.url = m1.url + m1.materialName;
            banners.push(material);
            //主页2
          } else if (res.data.data[i].creatorType == "2"){
            var m1 = res.data.data[i];
            material.id = m1.id;
            material.url = m1.url + m1.materialName;
            envpics.push(material);
          }
        }
        that.setData({
          banners: banners,
          envpic: envpics
        });
      }
    });

    //关于我们
    wx.request({
      url: app.globalData.surl +'wx-cms/getMessage',
      data : {
        key: 'aboutUs',
        jkum: app.globalData.jkum
      },
      success: function (res){

        var result = res.data;
       
        if(result.ok){
          that.setData({
            aboutusfirst: result.data[0].desc
          });
        }else{
          wx.showModal({
            title: '失败',
            content: '后台连接失败，无法查询数据...........',
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: wx.getStorageSync('mallName')
    }
  },

  //事件处理函数
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },

  coachswiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },

  showmap:function (){
    var mallName = wx.getStorageSync('mallName');
    var latitude = parseFloat(wx.getStorageSync('latitude'));
    var longitude = parseFloat(wx.getStorageSync('longitude'));
    this.openLocationFun(latitude, longitude, 15, mallName,"");
  },
  showvideo:function (){
    wx.navigateTo({
      url: '/pages/video/video',
    });
  },
  /**  
 * 使用微信内置地图查看位置  
 * 1、latitude：     纬度，范围为-90~90，负数表示南纬 必填  
 * 2、longitude：    经度，范围为-180~180，负数表示西经 必填  
 * 3、scale：        缩放比例，范围1~28，默认为28 选填  
 * 4、name：         位置名 选填  
 * 5、address：      地址的详细说明 选填  
 * 6、cbSuccessFun： 接口调用成功的回调函数 选填  
 * 7、cbFailFun：    接口调用失败的回调函数 选填  
 * 8、cbCompleteFun：接口调用结束的回调函数（调用成功、失败都会执行） 选填  
 */
  openLocationFun: function(latitude, longitude, scale, name, address, cbSuccessFun, cbFailFun, cbCompleteFun){  
    var openObj= {};  
    openObj.latitude = latitude;  
    openObj.longitude = longitude;  
    openObj.scale = 15;  
    if(scale>0 && scale < 29) {
      openObj.scale = scale;
    }  
    if(name) {
      openObj.name = name;
    }  
    if(address) {
      openObj.address = address;
    }  
    openObj.success = function (res) {
      if (cbSuccessFun) {
        cbSuccessFun();
      }
    }  
    openObj.fail = function (res) {
      if (cbFailFun) {
        cbFailFun();
      } else {
        console.log("openLocation fail:" + res.errMsg);
      }
    }  
    openObj.complete = function (res) {
      if (cbCompleteFun) {
        cbCompleteFun();
      }
    }  
    wx.openLocation(openObj);
  }
  //查看全部教练
  // ,toAllCoachesTap:function (){
  //   wx.navigateTo({
  //     url: "/pages/coaches/coaches?coachid=" + this.data.coachcategoryid
  //   })
  // }
})